/**
* 用于各种类型转换
* 直接调用方法
**/
package goutil

import (
	"fmt"
	"strconv"
)
//转换字符串类型
func ToString(val interface{}) string {
	switch val.(type) {
	case string:
		return val.(string)
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		return fmt.Sprintf("%d", val)
	case float32:
		return strconv.FormatFloat(float64(val.(float32)), 'f', -1, 64)
	case float64:
		return strconv.FormatFloat(val.(float64), 'f', -1, 64)
	default:
		return ""
	}
	return ""
}

//将数值转换为int64
func ToInt64(val interface{}) int64 {
	switch val.(type) {
	case int:
		return int64(val.(int))
	case int8:
		return int64(val.(int8))
	case int16:
		return int64(val.(int16))
	case int32:
		return int64(val.(int32))
	case int64:
		return int64(val.(int))
	case float32:
		return int64(val.(float32))
	case float64:
		return int64(val.(float64))
	case string:
		v, _ := strconv.ParseFloat(val.(string), 64)
		return int64(v)
	default:
		return int64(0)
	}
	return int64(0)
}

func ToFloat64(val interface{}) float64 {
	switch val.(type) {
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		v, _ := strconv.ParseFloat(ToString(val), 64)
		return v
	case string:
		v, _ := strconv.ParseFloat(val.(string), 64)
		return v
	default:
		return float64(0)
	}
	return float64(0)
}